package com.tsmti.system.service.impl;


import com.tsmti.system.entity.User;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * Title：PimsSystem
 * Class：com.ts.sys.service.impl
 * Comapny:
 *
 * @author：zcw
 * @version：2.5 CreateTime：2018/1/17 17:08
 * Modify log:
 * Description：
 */

@Service("passwordService")
public class PasswordService {

    private RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();

    @Value(value = "${shiro.credentials.hashAlgorithmName}")
    private String algorithmName = "md5";
    @Value(value = "${shiro.credentials.hashIterations}")
    private final int hashIterations = 2;

    public void encryptPassword(User user) {
        user.setSalt(randomNumberGenerator.nextBytes().toHex());
        String newPassword = new SimpleHash(algorithmName, user.getPassword(),
                ByteSource.Util.bytes(user.getCredentialsSalt()), hashIterations).toHex();
        user.setPassword(newPassword);
    }
}
